package com.whq.knowverywell.business.db.helper;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.whq.knowverywell.business.db.TblConfHandler;
import com.whq.knowverywell.business.db.TblUserHandler;
import com.whq.knowverywell.util.LogUtil;

/**
 * 
 * 封装对SQLite数据库的操作.
 * 
 * @author wanghuaiqiang
 */
public class MainSQLiteOpenHelper extends SQLiteOpenHelper {
    private static final String TAG = "MainSQLiteOpenHelper";

    private static final int VERSION = 3;

    private static final String DB_NAME = "main.db";

    public MainSQLiteOpenHelper(Context context) {
        super(context, DB_NAME, null, VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
    	// 配置
        db.execSQL(TblConfHandler.CREATE_TABLE_SQL);

        // 用户信息
        db.execSQL(TblUserHandler.CREATE_TBL_USER_SQL);
        
        LogUtil.d(TAG, " database created.");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    	if (oldVersion <= 2) {
    		db.execSQL("DROP TABLE IF EXISTS " + TblConfHandler.TBL_CONF + ";");
            db.execSQL(TblConfHandler.CREATE_TABLE_SQL);
            
            db.execSQL("DROP TABLE IF EXISTS " + TblUserHandler.TBL_USER + ";");
            db.execSQL(TblUserHandler.CREATE_TBL_USER_SQL);
    	}
    	
        LogUtil.d(TAG, " database upgraded.");
    }

}
